/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser for the 'p4 fstat' command.
  * 
  * @version $Revision: 67 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Fstat extends AbstractPerforceCommand implements IPerforceCommand
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the result of the operation. */
	private List result = new ArrayList();
	
	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Convenience method for creating instances of Fstat.
	  * 
	  * @param monitor for progress feedback.
	  * 
	  * @return the new Fstat instance.
	  */
	public static final Fstat newInstance(IProgressMonitor monitor)
	{
		MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null); 
		return new Fstat(status, monitor);
	}
	
	// ------------------------------------------------------------------------ Private Constructors

	/** Creates a new instance.
	  * 
	  * @param status the MultiStatus object.
	  * @param monitor for progress feedback.
	  */
	private Fstat(MultiStatus status, IProgressMonitor monitor)
	{
		super(status);
	}
	
	// -------------------------------------------------------- Public Methods (IClientUserDelegate)
	
	/** @see IClientUserDelegate#outputStat(Map) */
	public void outputStat(Map strDict)
	{
		getProgressMonitor().subTask(Helper.safeGetString(strDict, "depotFile"));      //$NON-NLS-1$
		
		ResourceSyncInfo resourceSyncInfo = new ResourceSyncInfo();						
		
		resourceSyncInfo.setAction(Helper.safeGetString(strDict, "action"));           //$NON-NLS-1$
		resourceSyncInfo.setDepotFileName(Helper.safeGetString(strDict, "depotFile")); //$NON-NLS-1$
		resourceSyncInfo.setHeadAction(Helper.safeGetString(strDict, "headAction"));   //$NON-NLS-1$
		resourceSyncInfo.setHeadType(Helper.safeGetString(strDict, "headType"));       //$NON-NLS-1$
		resourceSyncInfo.setHeadTime(Helper.safeGetDate(strDict, "headTime"));         //$NON-NLS-1$
		resourceSyncInfo.setHeadRev(Helper.safeGetInteger(strDict, "headRev"));        //$NON-NLS-1$
		resourceSyncInfo.setHeadChange(Helper.safeGetInteger(strDict, "headChange"));  //$NON-NLS-1$
		if (strDict.containsKey("change"))
		{
			resourceSyncInfo.setChange(Helper.safeGetChangelist(strDict, "change"));   //$NON-NLS-1$
		}
		resourceSyncInfo.setHaveRev(Helper.safeGetInteger(strDict, "haveRev"));        //$NON-NLS-1$
		resourceSyncInfo.setClientFileName(
			Helper.safeGetString(strDict, "clientFile"));                              //$NON-NLS-1$

		if (strDict.containsKey("unresolved"))                                         //$NON-NLS-1$
		{
			resourceSyncInfo.setResolved(Boolean.FALSE);
		}
		
		result.add(resourceSyncInfo);
		
		getProgressMonitor().worked(1);
	}

	// ----------------------------------------------------------- Public Methods (IPerforceCommand)

	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "fstat";                                                                //$NON-NLS-1$
	}
	
	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus()
	{
		return getMultiStatus();
	}	
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}		
	
	// ------------------------------------------------------------------------------ Public Methods
	
	/** Returns the result of the operation.
	  * 
	  * @return array of ResourceSyncInfo's.
	  */
	public ResourceSyncInfo[] getResult()
	{
		return (ResourceSyncInfo[]) result.toArray(new ResourceSyncInfo[result.size()]);
	}
}
